package com.huawei.esdk.uc.device.u19.ctc.convert;

import java.util.ArrayList;
import java.util.List;
import com.huawei.ecs.bin.service.AddConf;
import com.huawei.ecs.bin.service.AddConfAck;
import com.huawei.ecs.bin.service.SetConf;
import com.huawei.ecs.bin.service.SetConfAck;
import com.huawei.ecs.bin.service.ShowConfAck;
import com.huawei.ecs.bin.service.data.Conf;
import com.huawei.ecs.bin.service.data.Conf.ConfMode;
import com.huawei.ecs.bin.service.data.Conf.SrtpMode;
import com.huawei.ecs.bin.service.data.ConfInfo;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.uc.domain.model.Conference;

public class CTCCapabilityConvert
{
    
    public AddConf createConfModal2South(Conference confParam)
    {
        AddConf addOConf = new AddConf();
        
        addOConf.confInfo_ = getConfInfoModal2South(confParam);
        
        return addOConf;
    }
    
    private ConfInfo getConfInfoModal2South(Conference confModal)
    {
        if (null == confModal)
        {
            return null;
        }
        
        ConfInfo confInfo = new ConfInfo();
        
        confInfo.startTime_ = DateUtils.dateToString(confModal.getStartTime(), "yyyy-MM-dd HH:mm:ss");
        confInfo.endTime_ = DateUtils.dateToString(confModal.getEndTime(), "yyyy-MM-dd HH:mm:ss");
        
        confInfo.conferenceNo_ = confModal.getConfRoom();
        confInfo.conferMode_ = ConfMode.get(Integer.parseInt(confModal.getConfMode()));
        
        confInfo.confName_ = confModal.getSubject();
        confInfo.attendPwd_ = confModal.getConfMemberPwd();
        confInfo.chairmanPwd_ = confModal.getConfChairPwd();
        
        confInfo.enterPrompt_ = new Byte(confModal.getEnterPrompt());
        confInfo.isConfRecord_ = confModal.isIfRecord();
        confInfo.isSuperConf_ = confModal.getIsSuperConf();
        confInfo.leavePrompt_ = new Byte(confModal.getLeavePrompt());
        confInfo.regSeatCount_ = (short)confModal.getMostNo();
        confInfo.speakerCount_ = new Byte(confModal.getMaxVoice());
        confInfo.srtpMode_ = SrtpMode.get(Integer.parseInt(confModal.getSrtpUsageMode()));
        confInfo.subPbxId_ = new Byte(confModal.getSubPbx());
        
        return confInfo;
    }

    private Conference getConfInfoSouth2Modal(Conf confSouth)
    {
        if (null == confSouth)
        {
            return null;
        }
        
        Conference confModal = new Conference();
        
        confModal.setStartTime(DateUtils.stringToDate(confSouth.startTime_, "yyyy-MM-dd HH:mm:ss"));
        confModal.setEndTime(DateUtils.stringToDate(confSouth.endTime_, "yyyy-MM-dd HH:mm:ss"));
        
        confModal.setConfRoom(confSouth.conferenceNo_);
        confModal.setConfMode(String.valueOf(confSouth.conferMode_.value()));
        
        confModal.setConfMemberPwd(confSouth.attendPwd_);
        confModal.setConfChairPwd(confSouth.chairmanPwd_);
        
        confModal.setEnterPrompt(String.valueOf(confSouth.enterPrompt_));
        
        confModal.setIfRecord(confSouth.isConfRecord_);
        confModal.setIsSuperConf(confSouth.isSuperConf_);
        confModal.setLeavePrompt(String.valueOf(confSouth.leavePrompt_));
        confModal.setMostNo(confSouth.regSeatCount_);
        confModal.setMaxVoice(String.valueOf(confSouth.speakerCount_));
        confModal.setSrtpUsageMode(String.valueOf(confSouth.srtpMode_.value()));
        confModal.setSubPbx(String.valueOf(confSouth.subPbxId_));
        
        confModal.setConfId(confSouth.conferId_);
        confModal.setAuthKey(confSouth.cryptKey_);
        confModal.setU19ConfType(confSouth.conferType_.value());
        confModal.setU19ConfStatus(confSouth.state_.value());
        
        return confModal;
    }
    
    public SetConf modifyConfModal2South(Conference confInfo)
    {
        SetConf setConf = new SetConf();
        
        setConf.confInfo_ = getConfInfoModal2South(confInfo);
        
        return setConf;
    }
    
    public SDKResult<String> createConfSouth2Modal(AddConfAck response)
    {
        SDKResult<String> sdkResult = new SDKResult<String>();
        
        final int successCodeLength = 10;
        final String successFlag = "10";
        String resCode = String.valueOf(response.retCode_);
        if (null != resCode && successCodeLength == resCode.length() && resCode.startsWith(successFlag))
        {
            sdkResult.setErrCode(0);
            sdkResult.setResult(resCode.substring(2));
        }
        else
        {
            sdkResult.setErrCode(response.retCode_);
        }
        sdkResult.setDescription(response.getCmdCode().name() + " " + response.getCmdCode().value());
        
        return sdkResult;
    }
    
    public SDKResult<String> modifyConfSouth2Modal(SetConfAck response)
    {
        SDKResult<String> sdkResult = new SDKResult<String>();
        sdkResult.setErrCode(response.retCode_);
        sdkResult.setDescription(response.getCmdCode().name() + " " + response.getCmdCode().value());
        return sdkResult;
    }
    
    public SDKResult<List<Conference>> queryConfSouth2Modal(ShowConfAck response)
    {
        SDKResult<List<Conference>> result = new SDKResult<List<Conference>>();
        
        result.setErrCode(response.retCode_);
        result.setDescription(response.getCmdCode().name() + " " + response.getCmdCode().value());
        
        if (0 == response.retCode_ && null != response.confLst_)
        {
            List<Conference> modalConfs = new ArrayList<Conference>();
            for (Conf item : response.confLst_)
            {
                modalConfs.add(getConfInfoSouth2Modal(item));
            }
            result.setResult(modalConfs);
        }
        
        return result;
    }
}
